package graphic;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileNotFoundException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Graph.Graph;

public class MyFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	/**
	 * @throws FileNotFoundException 
	 * 
	 */

	public MyFrame(Graph City) throws FileNotFoundException {
		super("Graph");
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(createFileMenu());
		setSize(1100, 800);
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		menuBar.add(Box.createHorizontalGlue());
		JLabel icon = new JLabel(new ImageIcon("images/download.gif"));
		icon.setBorder(BorderFactory.createLoweredBevelBorder());
		menuBar.add(icon);
		
		JToolBar tool = new JToolBar();
		
		setPreferredSize(new Dimension(1000, 700));
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		final MyPanel content = new MyPanel(City);
		content.setBackground(Color.white);
		JSlider speed = new JSlider(0,100);
		JSlider maxCars = new JSlider(0,1000);
		speed.setValue(10);
		speed.setMajorTickSpacing(10);
		speed.setMinorTickSpacing(5);
		speed.setPaintTicks(true);
		speed.setPaintLabels(true);
		speed.setPaintTicks(true);
		speed.setPaintTrack(true);
		speed.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int value = 10*((JSlider)e.getSource()).getValue();
			double speed = (double)((double)value/(double)100);
			graphic.SingleCar.setSpeed(speed);
			
			}
			});
		maxCars.setValue(100);
		maxCars.setMajorTickSpacing(100);
		maxCars.setMinorTickSpacing(50);
		maxCars.setPaintTicks(true);
		maxCars.setPaintLabels(true);
		maxCars.setPaintTicks(true);
		maxCars.setPaintTrack(true);
		maxCars.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					int value = ((JSlider)e.getSource()).getValue();
				int maxCars = value;
				content.setmaxCars(maxCars);
				
				}
				});
		
		tool.add(new JLabel("Speed:"));
		tool.add(speed);
		tool.add(new JLabel("Max Cars:"));
		tool.add(maxCars);
		JScrollPane mainP = new JScrollPane(content);
		
		mainP.setViewportBorder(BorderFactory.createLineBorder(Color.blue));
		mainP.setWheelScrollingEnabled(true);
		mainP.setPreferredSize(new Dimension(1200, 800));
		mainP.setViewportBorder(
                BorderFactory.createLineBorder(Color.black));
		mainP.setBorder(mainP.getBorder());
		mainPanel.add(mainP,BorderLayout.CENTER);
		mainPanel.add(tool,BorderLayout.NORTH);
		
		getContentPane().add(mainPanel);
		
		
		Timer timer = new Timer(20, new ActionListener()
		{
            public void actionPerformed(ActionEvent ev) {
                repaint();
            }
		});
		timer.start();
	}
	
	void paintComponent(Graphics g){}
	
	private JMenu createFileMenu() {
		JMenu file = new JMenu("file");
		JMenuItem open = new JMenuItem("Open", new ImageIcon("images/open16.gif"));
		JMenuItem exit = new JMenuItem(new ExitAction());
		file.add(open);
		file.addSeparator();
		file.add(exit);
		return file;
	}
	class ExitAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		ExitAction() {
			putValue(NAME, "exit!!!");
		}
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
	
	
	class ListenerFactory {
		
		public WindowListener getWindowL() {
			return new WindowL();
		}
		class WindowL extends WindowAdapter {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		}
	}
}
	

